// SelectNodeDlg.cpp :
//

#include "stdafx.h"
#include "SelectNodeDlg.h"
#include "Global.h"


IMPLEMENT_DYNAMIC(CSelectNodeDlg, CDialog)

CSelectNodeDlg::CSelectNodeDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSelectNodeDlg::IDD, pParent),
  m_pSelectSet(NULL),
  m_lCurrent(0)
{

}

CSelectNodeDlg::~CSelectNodeDlg()
{
}

BOOL CSelectNodeDlg::OnInitDialog()
{
	SetWindowLong(m_hWnd,GWL_EXSTYLE,GetWindowLong(m_hWnd,GWL_EXSTYLE)^WS_EX_LAYERED);
	SetLayeredWindowAttributes(RGB(0,0,0), 80, LWA_ALPHA);

	CSKGlobal::GetHighLightSet()->Reset();

	CPoint mousePt;
	::GetCursorPos(&mousePt);

	CRect dlgRect;
	GetWindowRect(&dlgRect);
	dlgRect.OffsetRect(mousePt.x - dlgRect.CenterPoint().x, mousePt.y - dlgRect.CenterPoint().y);
	MoveWindow(&dlgRect);

	m_pSelectSet = CSKGlobal::GetSelectSet();

	CWnd* pPreBtn = GetDlgItem(IDC_BTN_SELECTNODE_PRE);
	CWnd* pNextBtn = GetDlgItem(IDC_BTN_SELECTNODE_NEXT);
	CWnd* pOkBtn = GetDlgItem(IDOK);

	long lCount = m_pSelectSet->GetCount();
	if (lCount == 0)
	{
		
		pPreBtn->EnableWindow(FALSE);
		pNextBtn->EnableWindow(FALSE);
		pOkBtn->EnableWindow(FALSE);
	}
	else if (lCount == 1)
	{
		pPreBtn->EnableWindow(FALSE);
		pNextBtn->EnableWindow(FALSE);
		pOkBtn->EnableWindow(TRUE);
	}
	else if (lCount == 2)
	{
		pPreBtn->EnableWindow(FALSE);
		pNextBtn->EnableWindow(TRUE);
		pOkBtn->EnableWindow(TRUE);
	}

	if (lCount > 0)
	{
		ISelect* pObject = m_pSelectSet->GetObject(m_lCurrent);
		CTransNode* pNode = dynamic_cast<CTransNode*>(pObject);
		CSKGlobal::GetHighLightSet()->Add(pNode);

		CSKGlobal::GetMainView()->UpdateView();
	}

	return CDialog::OnInitDialog();
}

void CSelectNodeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_BTN_SELECTNODE_PRE, m_BtnPrev);
	DDX_Control(pDX, IDC_BTN_SELECTNODE_NEXT, m_BtnNext);
	DDX_Control(pDX, IDOK, m_BtnSelectNodeOK);
}


BEGIN_MESSAGE_MAP(CSelectNodeDlg, CDialog)
	ON_BN_CLICKED(IDC_BTN_SELECTNODE_PRE, &CSelectNodeDlg::OnBnClickedBtnSelectnodePre)
	ON_BN_CLICKED(IDOK, &CSelectNodeDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDC_BTN_SELECTNODE_NEXT, &CSelectNodeDlg::OnBnClickedBtnSelectnodeNext)
END_MESSAGE_MAP()

void CSelectNodeDlg::OnBnClickedBtnSelectnodePre()
{
	CSKGlobal::GetHighLightSet()->Reset();

	if (m_lCurrent == 0)
	{
		return;
	}
	m_lCurrent --;

	if (m_lCurrent == 0)
	{
		ResetBtns();
		m_BtnPrev.EnableWindow(FALSE);
	}

	ISelect* pObject = m_pSelectSet->GetObject(m_lCurrent);
	CTransNode* pNode = dynamic_cast<CTransNode*>(pObject);
	CSKGlobal::GetHighLightSet()->Add(pNode);

	CSKGlobal::GetMainView()->UpdateView();
}

void CSelectNodeDlg::OnBnClickedOk()
{
	CDragNodeProcessCard* pDragNodeDef = CSKGlobal::GetTargetDoc()->GetDragNodeDef();
	ISelect* pObject = m_pSelectSet->GetObject(m_lCurrent);
	CTransNode* pNode = dynamic_cast<CTransNode*>(pObject);
	pDragNodeDef->SetSelectObj(pNode);

	OnOK();

	CPoint mousePt;
	::GetCursorPos(&mousePt);
	pDragNodeDef->SetStartDragPt(mousePt.x, mousePt.y);
}

void CSelectNodeDlg::OnBnClickedBtnSelectnodeNext()
{
	CSKGlobal::GetHighLightSet()->Reset();

	if (m_lCurrent == m_pSelectSet->GetCount() - 1)
	{
		return;
	}
	m_lCurrent ++;

	if (m_lCurrent == m_pSelectSet->GetCount() - 1)
	{
		ResetBtns();
		m_BtnNext.EnableWindow(FALSE);
	}

	ISelect* pObject = m_pSelectSet->GetObject(m_lCurrent);
	CTransNode* pNode = dynamic_cast<CTransNode*>(pObject);
	CSKGlobal::GetHighLightSet()->Add(pNode);

	CSKGlobal::GetMainView()->UpdateView();
}

void CSelectNodeDlg::ResetBtns()
{
	m_BtnPrev.EnableWindow(TRUE);
	m_BtnNext.EnableWindow(TRUE);
	m_BtnSelectNodeOK.EnableWindow(TRUE);
}